<?php

namespace backend\controllers;

//require_once 'vendor/autoload.php';

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\Style\Table;
use Yii;
use yii\helpers\Url;

use PhpOffice\PhpWord\Element\Table as Table2;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\Style\Cell;

class PhpOfficeController extends BaseController
{
    /**
     * @desc 下载doc php-office/down-word1
     * @return void
     * @Date: 2025/7/7
     */
    public function actionDownWord1()
    {
        // 创建 PhpWord 实例
        $phpWord = new PhpWord();

        // 添加一个节
        $section = $phpWord->addSection();

        // 添加表格标题
        $section->addText('员工信息表', ['bold' => true, 'size' => 16], ['alignment' => 'center']);

        // 创建表格
        $table = $section->addTable([
            'borderSize' => 6,
            'borderColor' => '006699',
            'cellMargin' => 50
        ]);

        // 添加表头
        $table->addRow();
        $table->addCell(2000)->addText('ID', ['bold' => true]);
        $table->addCell(2000)->addText('姓名', ['bold' => true]);
        $table->addCell(2000)->addText('职位', ['bold' => true]);
        $table->addCell(2000)->addText('薪资', ['bold' => true]);

        // 添加表格数据（通常从数据库获取）
        $employees = [
            [1, '张三', '项目经理', '15000'],
            [2, '李四', '开发工程师', '12000'],
            [3, '王五', 'UI设计师', '10000'],
        ];

        foreach ($employees as $employee) {
            $table->addRow();
            foreach ($employee as $cell) {
                $table->addCell()->addText($cell);
            }
        }

        // 保存 Word 文档
        $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save('employees_word1.docx');

        echo 'Word 文档已生成：employees.docx';

    }

    /**
     * @desc 下载doc php-office/down-word2
     * @return void
     * @Date: 2025/7/7
     */
    public function actionDownWord2()
    {
        $phpWord = new PhpWord();
        $section = $phpWord->addSection();

        // 添加标题
        $section->addText(
            '公司员工报表',
            ['name' => 'Arial', 'size' => 18, 'bold' => true, 'color' => '1B2232'],
            ['alignment' => 'center', 'spaceAfter' => Converter::pointToTwip(12)]
        );

        // 模拟数据库数据
        $employees = [
            ['id' => 1, 'name' => '张三', 'position' => '项目经理', 'salary' => 15000, 'hire_date' => '2020-01-15'],
            ['id' => 2, 'name' => '李四', 'position' => '高级开发', 'salary' => 12000, 'hire_date' => '2019-05-20'],
            ['id' => 3, 'name' => '王五', 'position' => 'UI设计师', 'salary' => 10000, 'hire_date' => '2021-03-10'],
        ];

        // 定义表格样式
        $tableStyle = [
            'borderColor' => '999999',
            'borderSize' => 6,
            'cellMargin' => 50,
            'alignment' => 'center'
        ];

        // 创建表格
        $table = $section->addTable($tableStyle);

        // 表头行
        $table->addRow(400);
        $headers = ['ID', '姓名', '职位', '薪资', '入职日期'];
        foreach ($headers as $header) {
            $table->addCell(2000, ['bgColor' => 'E6E6E6', 'valign' => 'center'])
                ->addText($header, ['bold' => true], ['alignment' => 'center']);
        }

        // 数据行
        foreach ($employees as $employee) {
            $table->addRow();
            $table->addCell()->addText($employee['id'], null, ['alignment' => 'center']);
            $table->addCell()->addText($employee['name']);
            $table->addCell()->addText($employee['position']);
            $table->addCell()->addText(number_format($employee['salary'], 2), null, ['alignment' => 'right']);
            $table->addCell()->addText($employee['hire_date'], null, ['alignment' => 'center']);
        }

        // 添加总计行
        $table->addRow();
        $table->addCell(2000, ['gridSpan' => 3, 'bgColor' => 'F2F2F2'])->addText('总计', ['bold' => true], ['alignment' => 'right']);
        $totalSalary = array_sum(array_column($employees, 'salary'));
        $table->addCell(2000, ['bgColor' => 'F2F2F2'])->addText(number_format($totalSalary, 2), ['bold' => true], ['alignment' => 'right']);
        $table->addCell(2000, ['bgColor' => 'F2F2F2']);

        // 保存文档
        $filename = 'employee_report_' . date('Ymd') . '.docx';
        $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save($filename);

        echo "文档已生成：<a href='$filename'>下载</a>";

    }

    public function actionDownWord3()
    {
        // 1. 加载模板
        $template = new TemplateProcessor('files/complex_table_template.docx');

        // 2. 替换简单变量
        $template->setValue('project_name', '电商平台升级项目');
        $template->setValue('report_date', date('Y年m月d日'));
        $template->setValue('reporter', '张项目经理');

        // 3. 创建复杂表格（包含各种合并单元格）
        $table = new Table2([
            'borderSize' => 6,
            'borderColor' => '000000',
            'width' => 100 * 50,
            'unit' => TblWidth::PERCENT,
            'alignment' => 'center'
        ]);

        // 3.1 添加表头行（横向合并）
        $table->addRow();
        $table->addCell(4000, ['gridSpan' => 4, 'bgColor' => 'D9D9D9', 'valign' => 'center'])
            ->addText('项目进度汇总表', ['bold' => true, 'size' => 14], ['alignment' => 'center']);

        // 3.2 添加列标题行
        $table->addRow();
        $table->addCell(1500, ['bgColor' => 'F2F2F2'])->addText('阶段', ['bold' => true]);
        $table->addCell(1500, ['bgColor' => 'F2F2F2'])->addText('任务', ['bold' => true]);
        $table->addCell(1500, ['bgColor' => 'F2F2F2'])->addText('负责人', ['bold' => true]);
        $table->addCell(1500, ['bgColor' => 'F2F2F2'])->addText('进度', ['bold' => true]);

        // 3.3 添加数据行（包含各种合并）
        // 第一组数据 - 需求阶段（纵向合并）
        $table->addRow();
        $table->addCell(1500, ['vMerge' => 'restart', 'valign' => 'center'])->addText('需求阶段');
        $table->addCell(1500)->addText('需求调研');
        $table->addCell(1500)->addText('张三');
        $table->addCell(1500)->addText('100%');

        $table->addRow();
        // 注意：vMerge = 'continue' 表示继续上一个纵向合并
        $table->addCell(1500, ['vMerge' => 'continue']);
        $table->addCell(1500)->addText('需求文档编写');
        $table->addCell(1500)->addText('李四');
        $table->addCell(1500)->addText('90%');

        // 第二组数据 - 开发阶段（混合合并）
        $table->addRow();
        $table->addCell(1500, ['vMerge' => 'restart', 'valign' => 'center'])->addText('开发阶段');
        $table->addCell(3000, ['gridSpan' => 2])->addText('前端开发', ['bold' => true]);
        $table->addCell(1500)->addText('75%');

        $table->addRow();
        $table->addCell(1500, ['vMerge' => 'continue']);
        $table->addCell(1500)->addText('用户模块');
        $table->addCell(1500)->addText('王五');
        $table->addCell(1500)->addText('100%');

        $table->addRow();
        $table->addCell(1500, ['vMerge' => 'continue']);
        $table->addCell(1500)->addText('订单模块');
        $table->addCell(1500)->addText('赵六');
        $table->addCell(1500)->addText('60%');

        // 3.4 添加总结行（横向合并）
        $table->addRow();
        $table->addCell(3000, ['gridSpan' => 2, 'bgColor' => 'E6E6E6'])->addText('当前总体进度', ['bold' => true]);
        $table->addCell(1500, ['bgColor' => 'E6E6E6'])->addText('平均', ['bold' => true]);
        $table->addCell(1500, ['bgColor' => 'E6E6E6'])->addText('85%', ['bold' => true]);

        // 4. 将复杂表格插入到模板
        $template->setComplexBlock('complex_table_placeholder', $table);

        // 5. 保存生成的文档
        $outputFile = 'project_report_' . date('Ymd_His') . '.docx';
        $template->saveAs($outputFile);

        echo "复杂表格文档生成成功：<a href='$outputFile'>下载查看</a>";
    }


}
